package com.example.podcastq;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.helloandroid.R;
import com.example.helloandroid.R.layout;

import Items.ElementoComentario;
import Items.ItemListBaseAdapterComentarios;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

public class SUBSCRIBE extends Activity {

	ArrayList<String> Lista ;
	Spinner spin;
	ImageButton send;
	EditText nombre, email, telefono, ciudad;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_subscribe);
//
//		spin = (Spinner) findViewById(R.id.combo);
		send = (ImageButton) findViewById(R.id.Sendnau);
		nombre = (EditText) findViewById(R.id.nombreedit);
		email = (EditText) findViewById(R.id.mailedit);
		telefono = (EditText) findViewById(R.id.telefonoedit);
		ciudad = (EditText) findViewById(R.id.ciudadedit);
		
		send.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {

				if(email.getText().length()>0&&nombre.getText().length()>1&&ciudad.getText().length()>0){
					new MeterseEnGrupos().execute(null);
				}
				else{
					Toast.makeText(SUBSCRIBE.this, "Rellena los campos obligatorios", Toast.LENGTH_LONG).show();
				}
				
			}
		});
		
		
		
		//new ObtenerGrupos().execute(null);
	}

	private class MeterseEnGrupos extends AsyncTask<String, String, String> {
		JSONObject recep;
		ProgressDialog dialog;
		@Override
		protected void onPostExecute(String result) {
			dialog.dismiss();
			Toast.makeText(SUBSCRIBE.this, "PROCESO COMPLETADO", Toast.LENGTH_SHORT);
			SUBSCRIBE.this.onBackPressed();
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			try {
				dialog = new ProgressDialog(SUBSCRIBE.this);
				dialog.setTitle(SUBSCRIBE.this.getString(R.string.cargando));
				dialog.setMessage(SUBSCRIBE.this.getString(R.string.obtInfoServer));
				dialog.setCancelable(false);
				dialog.show();
			} catch (final Exception e) {
				Log.e("Error", e.getMessage());
			}

		}
		@Override
		protected String doInBackground(String... params) {
//			&merge_vars[GROUPINGS][0][id]=6633&merge_vars[GROUPINGS][0][groups]="+params[2]
			try {
				URL url = new  URL("http://us6.api.mailchimp.com/1.3/?method=listSubscribe&apikey=847861beb144132d7491f43c032cc9af-us6&id=9ad5f6a5f4&email_address="+email.getText()+"&merge_vars[FNAME]="+nombre.getText()+"&merge_vars[PHONE]="+telefono.getText()+"&send_welcome=true" );
				Lista = new ArrayList<String>();
				InputStream is = url.openStream();
				is.close();


			} catch (Exception e) {
				// TODO Auto-generated catch block
				return "KO";
			}
			return "OK";
		}

	}
//	private class ObtenerGrupos extends AsyncTask<String, String, String> {
//		JSONArray recep;
//		ProgressDialog dialog;
//		ArrayAdapter<String> adapter;
//		@Override
//		protected void onPostExecute(String result) {
//			dialog.dismiss();
//			adapter = new ArrayAdapter<String>(SUBSCRIBE.this,
//					android.R.layout.simple_spinner_item, Lista);
//			spin.setAdapter(adapter);
//			
//		}
//		@Override
//		protected void onPreExecute() {
//			// TODO Auto-generated method stub
//			super.onPreExecute();
//			try {
//				dialog = new ProgressDialog(SUBSCRIBE.this);
//				dialog.setTitle(SUBSCRIBE.this.getString(R.string.cargando));
//				dialog.setMessage(SUBSCRIBE.this.getString(R.string.obtInfoServer));
//				dialog.setCancelable(false);
//				dialog.show();
//			} catch (final Exception e) {
//				Log.e("Error", e.getMessage());
//			}
//
//		}
//		@Override
//		protected String doInBackground(String... params) {
//
//			try {
//				URL url = new  URL("http://us6.api.mailchimp.com/1.3/?method=listInterestGroupings&apikey=847861beb144132d7491f43c032cc9af-us6&id=f5be33ed71");
//				Lista = new ArrayList<String>();
//				InputStream is = url.openStream();
//				String result;
//				BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
//				StringBuilder sb = new StringBuilder();
//				String line = null;
//				while ((line = reader.readLine()) != null) {
//					sb.append(line);
//				}
//				is.close();
//				result=sb.toString();
//
//				recep = new JSONArray(result);
//
//				for(int i = 0; i < recep.getJSONObject(0).getJSONArray("groups").length(); i ++){
//					Lista.add(
//							recep.getJSONObject(0).getJSONArray("groups").getJSONObject(i).getString("name").toString()
//							);
//				}
//
//
//			} catch (MalformedURLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (JSONException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			return null;
//		}
//
//	}


}


